package org.extframework.ld.http.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public abstract class ContentListener implements ServletContextListener {
	
	protected Log logger = LogFactory.getLog(super.getClass());
	protected ApplicationContext springApplicationContext;
	protected ServletContextEvent servletContextEvent;
	
	public void contextDestroyed(ServletContextEvent contextEvent) {
		logger.info("application contextDestroyed start");
		servletContextEvent = contextEvent;
		contextDestroyed();
		logger.info("application contextDestroyed end");
	}

	public void contextInitialized(ServletContextEvent contextEvent) {
		if (this.springApplicationContext == null) {
			this.springApplicationContext = WebApplicationContextUtils
					.getWebApplicationContext(contextEvent.getServletContext());
		}
		logger.info("application contextInitialized start");
		servletContextEvent = contextEvent;
		contextInitialized();
		logger.info("application contextInitialized end");
	}
	
	
	protected <T> T getBean(Class<T> paramClass) {
		return this.springApplicationContext.getBean(paramClass);
	}

	protected Object getBean(String beanId) {
		return this.springApplicationContext.getBean(beanId);
	}
	
	protected abstract void contextDestroyed();

	protected abstract void contextInitialized();
	
}
